**
** This script plots all of the figures in the text.
**
**
**
** 
** 




capture log close
log using event-study.log, replace

set more off
set scheme s1color
clear
clear

graph drop _all
program drop _all

local font "Garamond"

foreach fmt in eps ps svg window {
	graph set `fmt' fontface "`font'"
	graph set `fmt' fontfacesans "`font'"
	graph set `fmt' fontfaceserif "`font'"
}

local color1 "maroon"
local color2 "gray"
local sym1 "circle"
local sym2 "triangle"


* appendix figure a3 - ceo turnover
		   
use "../../Darwin's Work/analysis/output/hos_analysis.dta", clear

codebook id_aha if exp_ever_type == 1
codebook id_aha if exp_ever_type == 2
codebook id_aha if exp_ever_type == 3
codebook id_aha if exp_ever_type == 4
codebook id_aha if exp_ever_type == 5

collapse (mean) new, ///
	by(ever_type year)
	
gen t = year-2006

twoway ///
	connected new t if ever_type == 0, ///
		color(`color1') msymbol(`sym1') ///
	|| ///
	connected new t if ever_type == 1, ///
		color(`color2') msymbol(`sym2') lpattern(dash) ///
	|| ///
	line new t if ever_type == 2, ///
		color(black) lpattern(dash_dot) ///
	|| ///
	pcarrowi 0.158 4.2 0.2 4.5, color(black) ///
	text(0.37 7 "Acquirer", placement(n) margin(small)) ///
	text(0.11 6.2 "Target", placement(s) margin(small)) ///
	text(0.165 4.2 "Other" "For-Profits", placement(s) margin(small)) ///
	xline(0.5, lcolor(black) lpattern(dash) noextend) ///
	title("") ///
	xlabel(-2(1)8) xscale( range(-2.5 8.5) nofextend ) ///
	xtitle("Year Relative to Merger (0=Year Before Merger)") ///
	ytitle("") legend( ///
	on order(- "Share of Hospitals" "with New CEO") pos(11) ring(0) size(*1.1) ///
	region(lstyle(none) margin(0 0 -10 0)) ///
	) ///
	ylabel(0(.15)0.45, format(%03.2f) angle(horizontal)) yscale( range(0 0.47) nofextend) ///
	plotregion( lstyle(none)) ///
	
graph export ceo_turnover.svg, replace
graph export ceo_turnover.pdf, replace

* figure 2 - ceo flows

use "../../Acq Analysis/AHA data/2019 AHA Request/transition_tables.dta", clear

keep if new_ceo_target==1 & year>=2004
collapse (sum) tr*, by(year)

gen tr_otnew=tr_external+tr_new_ceo
drop tr_external tr_new_ceo

* make cumulative versions for area plot
gen cumu_target = tr_target
gen cumu_acquirer = cumu_target + tr_acq
gen cumu_otnew = cumu_acquirer + tr_otnew

gen t = year-2005.9

gen tminus = t-0.5
sort tminus

qui summ tminus
local n = r(N)
local nplus = r(N) + 1
set obs `nplus'
replace tminus = tminus[_n-1]+1 if _n==`nplus'

foreach var of varlist cumu_* {
	replace `var' = `var'[_n-1] if _n==`nplus'
}
sort tminus

twoway ///
	bar cumu_target t , ///
	fintensity(inten100) color(`color1'*.2) barwidth(1.01) ///
	|| ///
	rbar cumu_acquirer cumu_target t , ///
	fintensity(inten100) color(`color2'*.2) barwidth(1.01) ///
	|| ///
	rbar cumu_otnew cumu_acquirer t , ///
	fintensity(inten100) lcolor(green%0) color(green*.20) barwidth(1.01) ///
	|| ///
	line cumu_acquirer cumu_target cumu_otnew tminus, ///
	lcolor(`color2' `color1' black) ///
	connect(stairstep stairstep stairstep) ///
	xlabel(-2(1)8) xscale( range(-2.5 8.5) nofextend ) ///
	xtitle("Year Relative to Merger (0=Year Before Merger)") ///
	legend( ///
	on order(- "Count of" "New CEOs") pos(11) ring(0) size(*1.1) ///
	region(lstyle(none) margin(0 0 0 1)) ///
	) ///
	text(0 0.5 "Previously CEO in Target", placement(n) margin(small) size(*.95)) ///
	text(0.5 5.5 "Previously CEO in Acquirer", placement(n) margin(small) size(*.95)) ///
	text(4.5 1 "Not Previously CEO" "in Target/Acquirer" , placement(n) margin(small) size(*.95)) ///
	ylabel(0(2)14, angle(horizontal)) yscale( nofextend) ///
	plotregion( lstyle(none)) ///
	|| ///
	pci 14 0.4 5.9 0.4, lcolor(black) lpattern(dash) ///
	|| ///
	pci 4.6 0.4 0.8 0.4, lcolor(black) lpattern(dash)
	
graph export ceo_flows.svg, replace
graph export ceo_flows.pdf, replace



* appendix figure a1 - stock price

use "../../Acq financials/Stock graph/stockPrice/data_clean/aggregate_daily.dta", clear

drop j k l

g timevar = yofd(date)
gen company_id = 0
replace company_id = 1 if name == "Acquirer Name"
replace company_id = 2 if name == "Target Name"
replace company_id = 3 if name == "Other Hospital System 1"
replace company_id = 4 if name == "Other Hospital System 2"
replace company_id = 5 if name == "Other Hospital System 3"
replace company_id = 6 if name == "S&P 500"

gen twoweeks = 1 if date > td(11mar2007) & date < td(27mar2007)
gen fourweeks = 1 if date > td(3mar2007) & date < td(3apr2007)

gen pre_announce = 1 if date < td(19mar2007)
gen pre_acquisition = 1 if date < td(25july2007)

keep if fourweeks == 1
gen close_day1 = 36.88 if company_id == 1
replace close_day1 = 48.81 if company_id == 2
replace close_day1 = 25.60 if company_id == 3
replace close_day1 = 28.78 if company_id == 4
replace close_day1 = 10.38 if company_id == 5
replace close_day1 = 1374.12 if company_id == 6

gen normalized = (close/close_day1) - 1

keep normalized date name

gen chain = "acquirer" if name=="Acquirer Name"
replace chain = "target" if name=="Target Name"
replace chain = "sp500" if name=="S&P 500"
drop name

keep if !missing(chain)

rename normalized normalized_
reshape wide normalized_, i(date) j(chain) string

* acquisition announced on Mar 19, 2007 - sif_date 17244
gen t = date-17244
gen t1 = t-0.05
gen t2 = t+0.05

local t_textyx "0.075 10"
local t_textpl "n"
local a_textyx "-.045 10"
local a_textpl "s"
//local yl "-.1(.02).1"
local yl `"0.1 "10%" 0.08 "8" 0.06 "6" 0.04 "4" 0.02 "2" 0.0 "0" -.02 "-2" -.04 "-4" -0.06 "-6" -.08 "-8" -.1 "-10" "'
local yfmt "%03.2f"
local title ""		

twoway ///
	connected ///
	normalized_target t1, ///
	lcolor(`color1') mcolor(`color1') msymbol(`sym1') ///
	|| ///
	connected ///
	normalized_acquirer t2, ///
	lcolor(`color2') lpattern(dash) mcolor(`color2') msymbol(`sym2') ///
	|| ///
	line ///
	normalized_sp500 t, ///
	lcolor(black) lpattern(shortdash) mcolor(black) ///
	text(`t_textyx' "Target", placement(`t_textpl') margin(small)) ///
	text(`a_textyx' "Acquirer", placement(`a_textpl') margin(small)) ///
	text(0.035 10 "S&P 500", placement(n) margin(small)) ///
	legend(off) xline(-.5, lcolor(black) lpattern(dash) noextend) yline(0,lcolor(black)) ///
	title("`title'", span) ///
	xlabel(-14(2)14) xscale( range(-14.5 14.5) nofextend ) ///
	xtitle("Day Relative to Merger Announcement (0 = Day of Announcement)") ///
	legend( ///
	on order(- "Return Relative to" "14 Days Before" "Merger Announcement") pos(11) ring(0) size(*1.1) ///
	region(lstyle(none) margin(0 0 0 1)) ///
	) ///
	ylabel(`yl', format(`yfmt') angle(horizontal)) yscale( nofextend) ///
	plotregion( lstyle(none))

graph export stock_returns.svg, replace
graph export stock_returns.pdf, replace


* appx figure a4 survival/readmission event studies

import excel using ../../Draft/tables/REStat_revision_tables.xls, ///
	sheet("Event studies survreadm") clear firstrow

gen group = cond(regexm(var,"^Acq"),"_acquirer","_target")

gen year = real(regexs(1)) if regexm(var,"([0-9]+)")
gen t = -1 if year==2003
replace t = 1 if year==2010
replace t = 2 if year==2014

assert !missing(t)
drop year

drop var Unadjusted* Numberofobservations
rename Adjusted* *

destring survival* readmission*, ignore("(*)") replace

foreach base in survival readmission {
	rename `base'se `base'_se
	rename `base'coef `base'_coef
	gen `base'_ci_lower = `base'_coef - invnormal(0.975)*`base'_se
	gen `base'_ci_upper = `base'_coef + invnormal(0.975)*`base'_se
}

order group t survival_* readmission_*

reshape wide *_coef *_se *_ci_*, i(t) j(group) string

local nplusone = _N+1
set obs `nplusone'

replace t = 0 if missing(t)
foreach var of varlist *_coef_* *_se_* *_ci_*_* {
	replace `var' = 0 if missing(`var')
}

sort t

gen t1 = t-0.05
gen t2 = t+0.05

foreach base in survival readmission {
	
	if ("`base'"=="survival") {
		local t_arrowyx "0.01 1.5 0.002 1"
		local t_textyx "0.01 1.5"
		local t_textpl "n"
		local a_arrowyx "-0.017 1.5 -0.0112 1.99"
		local a_textyx "-0.016 1.5"
		local a_textpl "s"
		local yl "-.020(.005).015"
		local yfmt "%04.3f"
		local title "A. Survival"
	}
	else if ("`base'"=="readmission") {
		local t_arrowyx "0.012 1.2 0.007 0.98"
		local t_textyx "0.012 1.2"
		local t_textpl "n"
		local a_arrowyx "-0.017 1.3 -0.0068 1.055"
		local a_textyx "-0.016 1.3"
		local a_textpl "s"
		local yl "-.020(.005).015"
		local yfmt "%04.3f"
		local title "B. Readmission"
	}
	else {
		exit
	}
	
	* stndalone version
	twoway ///
		rarea ///
		`base'_ci_upper_target `base'_ci_lower_target t1, ///
		cmissing(n) fcolor(`color1'%20) lcolor(`color1'%0) lalign(outside) ///
		|| ///
		rarea ///
		`base'_ci_upper_acquirer `base'_ci_lower_acquirer t2, ///
		cmissing(n) fcolor(`color2'%20) lcolor(`color2'%0) lalign(outside) ///
		|| ///
		connected ///
		`base'_coef_target t1, ///
		cmissing(n) lcolor(`color1') mcolor(`color1') msymbol(`sym1') ///
		|| ///
		connected ///
		`base'_coef_acquirer t2, ///
		cmissing(n) lcolor(`color2') lpattern(dash) mcolor(`color2') msymbol(`sym2') ///
		|| ///
		pcarrowi `t_arrowyx', ///
		color(black) ///
		text(`t_textyx' "Target", placement(`t_textpl') margin(small)) ///
		|| ///
		pcarrowi `a_arrowyx', ///
		color(black) ///
		text(`a_textyx' "Acquirer", placement(`a_textpl') margin(small)) ///
		legend(off) xline(0.5, lcolor(black) lpattern(dash) noextend) ///
		yline(0,lcolor(black) lwidth(*.85)) ///
		title("`title'", span size(*.9)) ///
		xlabel(-1(1)2) xscale( range(-1.2 2.2) nofextend ) ///
		xtitle("Period Relative to Merger (0=Period Before Merger)") ///
		legend( ///
		on order(- "Coefficient") pos(11) ring(0) size(*1.1) ///
		region(lstyle(none) margin(0 0 0 1)) ///
		) ///
		ylabel(`yl', format(`yfmt') angle(horizontal)) yscale( nofextend) ///
		plotregion( lstyle(none)) ///
		name(`base') nodraw
}

graph combine survival readmission, ///
	xsize(7) ysize(3) iscale(*1.7) altshrink ///
	graphregion(margin(none)) plotregion(margin(none)) imargin(none)
graph export appx_survreadm.svg, replace
graph export appx_survreadm.pdf, replace

* figure 1a - adoption of acquirer EMR

import delimited using ../HIMSS/output/acq_emr_adoption_for_adam.csv, clear

replace acq_gr_name = lower(acq_gr_name)
drop acq_gr
rename coef coef_
reshape wide coef, i(year) j(acq_gr_name) string

gen t = year-2006
gen t1 = t-0.05
gen t2 = t+0.05

local t_textyx "0.6 6.8"
local t_textpl "n"
local a_textyx "0.9 6.8"
local a_textpl "s"
local yl "0(.2)1"
local yfmt "%03.2f"
local title "A. Adoption of Acquirer-Linked EMR"		

twoway ///
	connected ///
	coef_target t1, ///
	lcolor(`color1') mcolor(`color1') msymbol(`sym1') ///
	|| ///
	connected ///
	coef_acquirer t2, ///
	lcolor(`color2') lpattern(dash) mcolor(`color2') msymbol(`sym2') ///
	text(`t_textyx' "Target", placement(`t_textpl') margin(small)) ///
	text(`a_textyx' "Acquirer", placement(`a_textpl') margin(small)) ///
	legend(off) xline(0.5, lcolor(black) lpattern(dash) noextend) ///
	title("`title'", span size(*.9)) ///
	xlabel(-3(1)8) xscale( range(-3.5 8.5) nofextend ) ///
	xtitle("Year Relative to Merger (0=Year Before Merger)") ///
	legend( ///
	on order(- "Share of" "Hospitals") pos(11) ring(0) size(*1.1) ///
	region(lstyle(none) margin(0 0 0 1)) ///
	) ///
	ylabel(`yl', format(`yfmt') angle(horizontal)) yscale( nofextend) ///
	plotregion( lstyle(none)) ///
	name(vendor_adopt) nodraw

* figures 1b-1d and appdx figure a2 - event studies

foreach plot in discvend_es wcostexit_es profit_margin_es logfte_es  wcostentrant_es lcapinvw_es  {
	if ("`plot'"=="discvend_es") {
		local t_arrowyx "-2.6 4 -2.1 4.5"
		local t_textyx "-2.5 4"
		local t_textpl "s"
		local a_arrowyx "-0.5 5 -1 4.6"
		local a_textyx "-0.5 5"
		local a_textpl "n"
		local yl "-4(1)1"
		local yfmt "%3.2f"
		local title "B. Event Study, No. of Discordant EMR Vendors"		
	}
	else if ("`plot'"=="wcostexit_es") {
		local t_arrowyx ".04 7 0.015 7.85"
		local t_textyx "0.04 7"
		local t_textpl "n"
		local a_arrowyx "-0.053 7 -0.015 7.9"
		local a_textyx "-0.05 7"
		local a_textpl "s"
		local yl "-.06(.03).09"
		local yfmt "%03.2f"
		local title "C. Event Study, Physician Exit Rate"
	}
	else if ("`plot'"=="profit_margin_es") {
		local t_arrowyx "0.03 6.8 0.01 6.3"
		local t_textyx "0.03 6.8"
		local t_textpl "n"
		local a_arrowyx "-0.07 6.1 -0.05 6.6"
		local a_textyx "-0.068 6.1"
		local a_textpl "s"
		local yl "-.08(.02).04"
		local yfmt "%03.2f"
		local title "D. Event Study, Profit Margin"
	}
	else if ("`plot'"=="wcostentrant_es") {
		local t_arrowyx "-0.024 7 0.01 7.9"
		local t_textyx "-0.02 7"
		local t_textpl "s"
		local a_arrowyx ".055 7 0.045 7.9"
		local a_textyx "0.057 7"
		local a_textpl "w"
		local yl "-.06(.03).09"
		local yfmt "%03.2f"
		local title "A. Physician Entry Rate"
	}
	else if ("`plot'"=="logfte_es") {
		local t_arrowyx "0.05 6.5 -0.03 6"
		local t_textyx "0.052 6.5"
		local t_textpl "n"
		local a_arrowyx "-0.2 6.5 -0.14 7"
		local a_textyx "-0.19 6.5"
		local a_textpl "s"
		local yl "-.2(.05).11"
		local yfmt "%03.2f"
		local title "B. Log of Full-Time Employment"		
	}
	else if ("`plot'"=="lcapinvw_es") {
		local t_arrowyx "-0.79 5 -0.45 4"
		local t_textyx "-0.75 5"
		local t_textpl "s"
		local a_arrowyx "0.54 3.6 0.2 4"
		local a_textyx "0.54 3.6"
		local a_textpl "n"
		local yl "-1.2(.4).8"
		local yfmt "%03.2f"
		local title "C. Log of 1+Capital Investment"		
	}

	
	use ../event-study-est/`plot'.dta, clear
	keep var coef ci_lower ci_upper
	drop if var=="_cons"
	
	* blank out coef & CI for variables that were dropped
	* (this is for capital investment in y=2003)
	foreach var of varlist coef ci_lower ci_upper {
		replace `var' = . if regexm(var,"^o\.")
	}
	
	* year should never be missing and should run from 1 to 12
	gen t = real(regexs(1)) if regexm(var,"([0-9]+)$")
	assert !missing(t)
	qui summ t, detail
	assert r(max)==12 & r(min)==1
	
	* re-index year
	replace t = t-4
	* t=0 should be omitted
	assert t!=0
	
	gen chain = regexs(2) if regexm(var,"^(o\.)?([^_]+)")
	drop var
	assert inlist(chain,"target_name","legacy")
	replace chain = "acquirer" if chain=="legacy"
	replace chain = "target" if chain=="target_name"
		
	* change to level of event time
	rename (coef ci_lower ci_upper) =_
	reshape wide coef_ ci_lower_ ci_upper_, i(t) j(chain) string
	
	* add an obs for t=0
	qui count
	local nobsplus = r(N)+1
	set obs `nobsplus'
	
	* the new obs should be zeroes for all vars
	foreach var of varlist _all {
		replace `var'=0 if _n==_N
	}
	
	* for flows measures, t=2 is not in the data. add it and set to missing
	if (inlist("`plot'","wcostentrant_es","wcostexit_es","wcostchurn_es")) {
		assert t!=2
		qui count
		local nobsplus = r(N)+1
		set obs `nobsplus'
		
		* the new obs should be missing for all vars except t
		foreach var of varlist _all {
			replace `var'=. if _n==_N
		}
		replace t=2 if _n==_N
	}
	
	sort t

	gen t1 = t-0.05
	gen t2 = t+0.05
	
	* stndalone version
	twoway ///
		rarea ///
		ci_upper_target ci_lower_target t1, ///
		cmissing(n) fcolor(`color1'%20) lcolor(`color1'%0) lalign(outside) ///
		|| ///
		rarea ///
		ci_upper_acquirer ci_lower_acquirer t2, ///
		cmissing(n) fcolor(`color2'%20) lcolor(`color2'%0) lalign(outside) ///
		|| ///
		connected ///
		coef_target t1, ///
		cmissing(n) lcolor(`color1') mcolor(`color1') msymbol(`sym1') ///
		|| ///
		connected ///
		coef_acquirer t2, ///
		cmissing(n) lcolor(`color2') lpattern(dash) mcolor(`color2') msymbol(`sym2') ///
		|| ///
		pcarrowi `t_arrowyx', ///
		color(black) ///
		text(`t_textyx' "Target", placement(`t_textpl') margin(small)) ///
		|| ///
		pcarrowi `a_arrowyx', ///
		color(black) ///
		text(`a_textyx' "Acquirer", placement(`a_textpl') margin(small)) ///
		legend(off) xline(0.5, lcolor(black) lpattern(dash) noextend) ///
		yline(0,lcolor(black) lwidth(*.85)) ///
		title("`title'", span size(*.9)) ///
		xlabel(-3(1)8) xscale( range(-3.5 8.5) nofextend ) ///
		xtitle("Year Relative to Merger (0=Year Before Merger)") ///
		legend( ///
		on order(- "Coefficient") pos(11) ring(0) size(*1.1) ///
		region(lstyle(none) margin(0 0 0 1)) ///
		) ///
		ylabel(`yl', format(`yfmt') angle(horizontal)) yscale( nofextend) ///
		plotregion( lstyle(none)) ///
		name(`plot') nodraw
		stop
}

graph combine wcostentrant_es logfte_es, ///
	xsize(7) ysize(3) iscale(*1.7) altshrink ///
	graphregion(margin(none)) plotregion(margin(none)) imargin(none)
graph export appx_figure_top.svg, replace
graph export appx_figure_top.pdf, replace

graph combine lcapinvw_es lcapinvw_es, ///
	xsize(7) ysize(3) iscale(*1.7) altshrink ///
	graphregion(margin(none)) plotregion(margin(none)) imargin(none)
graph export appx_figure_bottom.svg, replace
graph export appx_figure_bottom.pdf, replace

graph combine vendor_adopt discvend_es, ///
	xsize(7) ysize(3) iscale(*1.7) altshrink ///
	graphregion(margin(none)) plotregion(margin(none)) imargin(none)
graph export main_figure_top.svg, replace
graph export main_figure_top.pdf, replace

graph combine wcostexit_es profit_margin_es, ///
	xsize(7) ysize(3) iscale(*1.7) altshrink ///
	graphregion(margin(none)) plotregion(margin(none)) imargin(none)
graph export main_figure_bottom.svg, replace
graph export main_figure_bottom.pdf, replace

log close
